package com.ybbase.framework.controller.flow.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybbase.framework.base.model.ResponseBean;
import com.ybbase.framework.common.constant.CommonResultCode;
import com.ybbase.framework.model.po.flow.ActionConfig;
import com.ybbase.framework.model.po.flow.NodeAction;
import com.ybbase.framework.service.flow.ActionConfigService;
import com.ybbase.framework.service.flow.NodeConfigService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程节点配置Controller
 * @author cg
 * @date 2019-11-04 19:14
 * @since 4.0
 */
@RestController
@RequestMapping("/flownodeconfig")
public class FlowNodeActionConfigController {

    private static final Logger logger = LogManager.getLogger(FlowNodeActionConfigController.class);

    @Autowired
    NodeConfigService nodeConfigService;
    @Autowired
    ActionConfigService actionConfigService;

    /**
     * 获取流程节点动作列表Action列表
     * @param actionConfig 动作配置
     * @return RequestMapping
     * @author cg
     * @date 2019-11-04 19:24
     * @since 4.0
     */
    @GetMapping("/findFlowNodeActionConfigList")
    @ApiOperation(value = "获取流程节点动作列表Action列表", notes = "获取流程节点动作列表Action列表")
    @ApiImplicitParam(name = "actionCode", value = "动作编码", dataType = "String")
    public ResponseBean findFlowNodeActionConfigList(ActionConfig actionConfig,@RequestParam(name = "page", defaultValue = "1") int pageIndex,
                                                     @RequestParam(name = "rows", defaultValue = "20") int step){
        Page<ActionConfig> page = new Page(pageIndex,step);
        try {
            QueryWrapper<ActionConfig> wrapper = new QueryWrapper<ActionConfig>();
            wrapper.lambda().eq(ActionConfig::getActionCode, actionConfig.getActionCode());
            actionConfigService.page(page , wrapper);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取流程节点动作列表Action列表异常", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "获取流程节点动作列表Action列表异常");

        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), page);
    }

    /**
     * 获取动作配置信息
     * @param actionId 动作配置节点Id
     * @return ResponseBean
     * @author cg
     * @date 2019-11-08 17:19
     * @since 4.0
     */
    @GetMapping("/getAction")
    @ApiOperation(value = "获取动作配置信息", notes = "获取动作配置信息")
    @ApiImplicitParam(name = "actionId", value = "动作配置节点Id", dataType = "String")
    public ResponseBean getAction(@RequestParam String actionId){
        ActionConfig actionConfig = new ActionConfig();
        try {
            actionConfig = actionConfigService.getById(Long.parseLong(actionId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取流程节点动作列表Action列表异常", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "获取流程节点动作列表Action列表异常",  new ActionConfig());
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), actionConfig);
    }

    /**
     * 获取流程节点动作列表Action列表 不带分页
     * @param
     * @return
     * @author cg
     * @date 2019-11-11 16:32
     * @since 4.0
     */
    @GetMapping("/getActionList")
    public ResponseBean getActionList(){
        List<ActionConfig> list = new ArrayList<>();
        try{
            list = actionConfigService.list();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取流程节点动作列表Action列表，不带分页", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "获取流程节点动作列表Action列表异常", new ArrayList<>());
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), list);
    }


    /**
     * 保存配置流程节点方法
     * @param actionConfig 动作配置表
     * @return RequestMapping
     * @author cg
     * @date 2019-11-04 19:36
     * @since 4.0
     */
    @PostMapping("/saveAction")
    @ApiOperation(value = "保存配置流程节点方法", notes = "保存配置流程节点方法")
    @ApiImplicitParam(name = "actionConfig", value = "动作配置表", dataType = "动作配置表")
    public ResponseBean saveAction(@RequestBody ActionConfig actionConfig){
        try {
            actionConfigService.save(actionConfig);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("保存配置流程节点方法异常", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "保存配置流程节点方法异常");
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), null);
    }

    /**
     * 编辑配置流程节点方法
     * @param actionConfig 动作配置表
     * @return RequestMapping
     * @author cg
     * @date 2019-11-04 19:38
     * @since 4.0
     */
    @PutMapping("/editAction")
    @ApiOperation(value = "编辑配置流程节点方法", notes = "编辑配置流程节点方法")
    @ApiImplicitParam(name = "actionConfig", value = "动作配置表", dataType = "动作配置表")
    public ResponseBean editAction(@RequestBody ActionConfig actionConfig){
        try {
            actionConfigService.updateById(actionConfig);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("编辑配置流程节点方法异常", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "编辑配置流程节点方法异常");
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), null);
    }

    /**
     * 删除配置流程节点方法
     * @param actionId 动作配置Id
     * @return RequestMapping
     * @author cg
     * @date 2019-11-04 19:40
     * @since 4.0
     */
    @DeleteMapping("/delAction")
    @ApiOperation(value = "删除配置流程节点方法", notes = "删除配置流程节点方法")
    @ApiImplicitParam(name = "actionConfig", value = "动作配置表", dataType = "动作配置表")
    public ResponseBean delAction(@RequestParam String actionId){
        try{
            ActionConfig actionConfig = actionConfigService.getById(Long.parseLong(actionId));
            List<NodeAction> list = actionConfigService.getNodeActionList(actionConfig.getActionCode());
            if(list!=null&&!list.isEmpty()){
                return new ResponseBean(CommonResultCode.FAIL.getCode(), "当前流程方法配置在流程节点下请请取消关联后再进行删除");
            }else {
                actionConfigService.removeById(Long.parseLong(actionId));
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除配置流程节点方法异常", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "删除配置流程节点方法异常");
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), null);
    }

    /**
     * 根据流程和节点编码获取节点动作
     * @param flowCode 流程编码
     * @param nodeCode 流程节点编码
     * @return
     * @author cg
     * @date 2019-11-04 19:56
     * @since 4.0
     */
    @GetMapping("/getActionByCondition")
    @ApiOperation(value = "根据流程和节点编码获取节点动作", notes = "根据流程和节点编码获取节点动作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flowCode", value = "流程编码", dataType = "String"),
            @ApiImplicitParam(name = "nodeCode", value = "节点编码", dataType = "String")
    })
    public ResponseBean getActionByCondition(@RequestParam String flowCode,@RequestParam String nodeCode){
        List<ActionConfig> list = new ArrayList<>();
        try {
            list = actionConfigService.getActionListByCode(flowCode,nodeCode);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("根据流程和节点编码获取节点动作异常", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "根据流程和节点编码获取节点动作");
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(), list);
    }





}
